package com.v1.video.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.common.ToastAlone;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.OtherPersonHomeActivity;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.ResultInfoEntity;
import com.v1.video.domain.SetHead;
import com.v1.video.domain.UserInfo4_4;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

import java.util.ArrayList;
import java.util.List;

public class MyLoginSubscribeAdapter extends BaseAdapter {

	private final String TAG = "MyLoginVideoAdapter";
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private List<UserInfo4_4> mLstData;
	private boolean isShowDelete = false;
	public static int count = 0;
	private Handler handler;
	private int flag = 0;
	public MyLoginSubscribeAdapter(Context context,Handler handler,int flag) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mLstData = new ArrayList<UserInfo4_4>();
		this.handler = handler;
		this.flag = flag;
	}

	public List<UserInfo4_4> getLstData() {
		return mLstData;
	}

	public void setLstData(List<UserInfo4_4> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}
	}
	
	public void setAddLstData(List<UserInfo4_4> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData.addAll(mLstData);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Holder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.item_person_info, null);
			holder = new Holder();
			holder.imageHead = (ImageView) convertView.findViewById(R.id.iv_user_img);
			holder.imgLevel = (ImageView) convertView.findViewById(R.id.iv_user_level);
			holder.nickName = (TextView) convertView.findViewById(R.id.tv_nickname);
			holder.userDes = (TextView) convertView.findViewById(R.id.paike_detail_cicle_fromname);
			holder.txtSubscribe = (TextView) convertView.findViewById(R.id.tv_attention);
			holder.layAttention = (LinearLayout)convertView.findViewById(R.id.lay_attention);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final UserInfo4_4 info = mLstData.get(position);
		SetHead setHead = new SetHead();
		setHead.setHead(info.getPicture(), "", holder.imageHead);
		if(info.getIsauth().equals("1")){
			holder.imgLevel.setVisibility(View.VISIBLE);
		}else{
			holder.imgLevel.setVisibility(View.GONE);
		}
		holder.nickName.setText(info.getUsername());
		holder.userDes.setText(info.getSignature());
		if(info.getType().equals("1")){
			holder.txtSubscribe.setText("已订阅");
		}else if(info.getType().equals("2")){
			holder.txtSubscribe.setText("相互订阅");
		}else{
			holder.txtSubscribe.setText("+订阅");
		}
		final int pos = position;
		holder.layAttention.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Constant.ATTENTION_TYPE_NOT.equals(info.getType())
						|| Constant.ATTENTION_TYPE_BY.equals(info.getType())) {
					getAddOrCancelFocous(info.getUid(),Constant.ATTENTION_TYPE_ADD,pos,holder);
				} else if (Constant.ATTENTION_TYPE_YET.equals(info
						.getType())
						|| Constant.ATTENTION_TYPE_TOGEHER.equals(info
								.getType())) {
					getAddOrCancelFocous(info.getUid(),Constant.ATTENTION_TYPE_CANCEL,pos,holder);
				}else{
					getAddOrCancelFocous(info.getUid(),Constant.ATTENTION_TYPE_ADD,pos,holder);
				}
			}
		});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(mContext)) {
					if(info.getUid().equals(LoginInfo.getInstance().getUserId())){
						
					}else{
						gotoOtherPersonHome(info.getUid(),info.getUsername(),info.getGender(),info.getIsauth(),info.getPicture(),info.getSignature(),info.getType());
					}
				} else {
					Toast.makeText(
							mContext,
							mContext.getResources().getString(
									R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return convertView;
	}

	private class Holder {
		public RelativeLayout layItem;
		public ImageView imageHead;
		public ImageView imgLevel;
		public TextView nickName;
		public TextView userDes;
		public TextView txtSubscribe;
		public LinearLayout layAttention;
	}
	
	public void gotoOtherPersonHome(String userId,String nickName,String userSex,String isCertification,String userHead,String userDesc,String focousType){
    	Intent intent = new Intent();
    	intent.setClass(mContext, OtherPersonHomeActivity.class);
    	intent.putExtra("userId", userId);
    	intent.putExtra("nickName", nickName);
    	intent.putExtra("userSex", userSex);
    	intent.putExtra("isCertification", isCertification);
    	intent.putExtra("userHead", userHead);
    	intent.putExtra("userDesc", userDesc);
    	intent.putExtra("focousType", focousType);
    	mContext.startActivity(intent);
    }
	
	 /**加关注或取消关注*/
    private void getAddOrCancelFocous(String focusUserId, final String typeId,final int pos,final Holder holder){
    	String url = V1VideoHttpApi.GET_VIDEO_TO_FOCOUS_ABOUT()+"&devid="+Constant.DEVICEID;
    	ParamList list = new ParamList();
    	list.add(new Parameter("fuid", focusUserId));
    	list.add(new Parameter("type", typeId));
    	list.add(new Parameter("uid", LoginInfo.getInstance().getUserId()));
		RequestManager.getInstance().getRequest(mContext, 1,url, list, ResultInfoEntity.class, 0, "MyLoginSubscribeAdapter", new OnResponseListener(){

			@Override
			public void onSuccess(Object object, int code, String message,String key) {
				// TODO Auto-generated method stub
				Logger.i(TAG, "onSuccess.object="+object.toString());
				
				if(object != null){
					ResultInfoEntity entity = (ResultInfoEntity)object;
					com.v1.video.domain.BaseEntity.Header header = entity.getHeader();
					if(header == null){
						ToastAlone.showToast((Activity)mContext, "未知错误", 1);
						return;
					}else if(!header.getStatus().equals("1")){
						ToastAlone.showToast((Activity)mContext, header.getMessage(), 1);
						return;
					}else if(header.getStatus().equals("1")){
						if(entity.getBody() != null){
							if(entity.getBody().getResult().getCode().equals(Constant.RESULT_CODE_SUCCESS)){
								String currentFocusOntype = mLstData.get(pos).getType();
								if (Constant.ATTENTION_TYPE_ADD.equals(typeId)) {
									// 加关注成功，变成已经关注
									if (Constant.ATTENTION_TYPE_NOT.equals(currentFocusOntype)) {
										mLstData.get(pos).setType(Constant.ATTENTION_TYPE_YET);
										holder.txtSubscribe.setText("已订阅");
									} else if (Constant.ATTENTION_TYPE_BY
											.equals(currentFocusOntype)) {
										mLstData.get(pos).setType(Constant.ATTENTION_TYPE_TOGEHER);
										holder.txtSubscribe.setText("相互订阅");
									}
								} else if (Constant.ATTENTION_TYPE_CANCEL.equals(typeId)) {
									// 取消关注成功，变成加关注
									if (Constant.ATTENTION_TYPE_YET.equals(currentFocusOntype)) {
										mLstData.get(pos).setType(Constant.ATTENTION_TYPE_NOT);
									} else if (Constant.ATTENTION_TYPE_TOGEHER
											.equals(currentFocusOntype)) {
										mLstData.get(pos).setType(Constant.ATTENTION_TYPE_BY);
									}
									holder.txtSubscribe.setText("+订阅");
								}
							}else{
								ToastAlone.showToast((Activity)mContext, entity.getBody().getResult().getMessage(),1);
							}
						}else{
//							toast(header.getMessage());
						}
					}
				}
			}

			@Override
			public void onFailure(int code, String message) {
				// TODO Auto-generated method stub
				Logger.i(TAG, message);
			}
		});
    }
}
